package com.itcodai.utils.stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.IntSummaryStatistics;
import java.util.stream.Collectors;

/**
 * Collectors工具类流使用
 * @author c-fanqj
 * @date 2022/5/15
 * @apiNote
 */
public class CollectorsDemo {

    public static void main(String[] args) {
        ArrayList<StreamExercise.Student> list = new ArrayList<>();
        Collections.addAll(list,
                new StreamExercise.Student("xiaoming", 89),
                new StreamExercise.Student("xiaobai", 98),
                new StreamExercise.Student("xiaohei", 78),
                new StreamExercise.Student("xiaolv", 86),
                new StreamExercise.Student("xiaowang", 59),
                new StreamExercise.Student("xiaoxiao", 100)
        );

        // maxBy-最大值 等同于max
        //  minBy-最小值 等同于min
        /*StreamExercise.Student max = list.stream()
                .collect(Collectors.maxBy((s1, s2) -> s1.getScore() - s2.getScore()))
                .get();
        StreamExercise.Student min = list.stream()
                .collect(Collectors.minBy((s1, s2) -> s1.getScore() - s2.getScore()))
                .get();
        System.out.println(max + " --- " + min);*/

        // joining 拼接字符串，分隔符，前后缀
        /*String s1 = list.stream()
                .map(StreamExercise.Student::getName)
                .collect(Collectors.joining());
        System.out.println(s1);
        String s2 = list.stream()
                .map(StreamExercise.Student::getName)
                .collect(Collectors.joining(","));
        System.out.println(s2);
        String s3 = list.stream()
                .map(StreamExercise.Student::getName)
                .collect(Collectors.joining(",", "{", "}"));
        System.out.println(s3);*/

        // summingInt averagingInt 映射成int类型数据，并求和、均
        int sum = list.stream()
                .collect(Collectors.summingInt(StreamExercise.Student::getScore));
        double average = list.stream()
                .collect(Collectors.averagingInt(StreamExercise.Student::getScore));
        //System.out.println(sum + " --- " + average);

        // summarizingInt 映射成int类型数据，并获取信息
        IntSummaryStatistics summaryStatistics = list.stream()
                .collect(Collectors.summarizingInt(StreamExercise.Student::getScore));
        /*System.out.println(summaryStatistics);
        //IntSummaryStatistics{count=6, sum=510, min=59, average=85.000000, max=100}
        System.out.println(summaryStatistics.getCount());
        System.out.println(summaryStatistics.getSum());
        System.out.println(summaryStatistics.getMin());
        System.out.println(summaryStatistics.getAverage());
        System.out.println(summaryStatistics.getMax());*/

    }

    /*static class Student {
        private String name;
        private int score;

        public String getName() {
            return name;
        }

        public int getScore() {
            return score;
        }

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        @Override
        public String toString() {
           *//*return "Student{" +
                   "name='" + name + '\'' +
                   ", age=" + age +
                   '}';*//*
            return String.format("姓名：%s，成绩：%d", name, score);
        }
    }*/

}
